<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 投诉管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
        <div class="handle-box">
                <div class="block">
                投诉日期:
                <el-date-picker
                    v-model="value1"
                    type="datetimerange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>

                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            </div>
            <el-table
            :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="shopId" label="商家编号" width="55" align="center" type="index"></el-table-column>
                <el-table-column prop="account" label="用户账号"></el-table-column>
                <el-table-column prop="userComplaints" label="用户投诉"></el-table-column>
                <el-table-column prop="businessComplaints" label="投诉处理"></el-table-column>
                <el-table-column prop="complaintsTime" label="评论时间" :formatter="formatDate"></el-table-column>
                <el-table-column prop="replayTime" label="回复时间" :formatter="fDate"></el-table-column>
                <el-table-column label="状态">
				    <template slot-scope="scope">
				        <el-tag
				            :type="scope.row.status===1?'success':'danger'"
				        >{{scope.row.status==0?'待处理':'已处理'}}</el-tag>
				    </template>
				</el-table-column>
                <el-table-column label="操作" width="150" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                        <el-button
                        v-show="scope.row.status == 1"
                            type="text"
                            icon="el-icon-view"
                            @click="handleShow(scope.$index, scope.row)"
                        >查看</el-button>
                        <el-button
                        v-show="scope.row.status == 0"
                            type="text"
                            icon="el-icon-view"
                            @click="handleEdit(scope.$index, scope.row)"
                        >处理</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        
        <!-- 查看弹出框 -->
        <el-dialog title="查看" :visible.sync="viewVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="商家编号">
                    <el-input v-model="form.id" :disabled='true'></el-input>
                </el-form-item>
                <el-form-item label="用户账号">
                    <el-input v-model="form.account" :disabled='true'></el-input>
                </el-form-item>
                <el-form-item label="用户评论">
                    <el-input   
                    type="textarea"
                    :rows="2" 
                    v-model="form.userComplaints" :disabled='true'></el-input>
                </el-form-item>
                <el-form-item label="投诉回复">
                    <el-input   
                    type="textarea"
                    :rows="2" 
                    v-model="form.businessComplaints" :disabled='true'></el-input>
                </el-form-item>
                <el-form-item label="评论时间">
                    <el-date-picker
                    format="yyyy-MM-dd HH:mm:ss"
                    :value="getRealTime(form.complaintsTime)"
                    type="datetime"
                    placeholder="选择日期时间"
                    :disabled="true">
                </el-date-picker>
                </el-form-item>
                <el-form-item label="回复时间">
                    <el-date-picker
                    format="yyyy-MM-dd HH:mm:ss"
                    :value="getRealTime(form.replayTime)"
                    type="datetime"
                    placeholder="选择日期时间"
                    :disabled="true">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="评论回复">
                    <el-select v-model="form.status" placeholder="请选择" class="handle-select mr10" :disabled='true'>
                        <el-option  label="待回复" :value="0"></el-option>
                        <el-option  label="已回复" :value="1"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="viewVisible = false">取 消</el-button>
            </span>
        </el-dialog>

        <!-- 处理弹出框 -->
        <el-dialog title="处理" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="商家编号">
                    <el-input v-model="form.id" :disabled='true'></el-input>
                </el-form-item>
                <el-form-item label="用户账号">
                    <el-input v-model="form.account" :disabled='true'></el-input>
                </el-form-item>
                <el-form-item label="用户评论">
                    <el-input   
                    type="textarea"
                    :rows="2" 
                    v-model="form.userComplaints" :disabled='true'></el-input>
                </el-form-item>
                <el-form-item label="投诉回复">
                    <el-input   
                    type="textarea"
                    :rows="2" 
                    v-model="form.businessComplaints" ></el-input>
                </el-form-item>
                <el-form-item label="评论时间">
                    <el-date-picker
                    format="yyyy-MM-dd HH:mm:ss"
                    :value="getRealTime(form.complaintsTime)"
                    type="datetime"
                    placeholder="选择日期时间"
                    :disabled="true">
                </el-date-picker>
                </el-form-item>
                <!--<el-form-item label="回复时间">
                    <el-date-picker
                    format="yyyy-MM-dd HH:mm:ss"
                    :value="getRealTime(form.replayTime)"
                    type="datetime"
                    placeholder="选择日期时间"
                    :disabled="true">
                    </el-date-picker>
                </el-form-item>-->
                <el-form-item label="评论回复">
                    <el-select v-model="form.status" placeholder="请选择" class="handle-select mr10" >
                        <el-option  label="待回复" :value="0"></el-option>
                        <el-option  label="已回复" :value="1"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button @click="saveEdit()">确 认</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {selectAllComplaints,selectAllByTime,delComplaints} from '../../api/complaints'
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                page: 1,
                pageSize: 10,
            },
            value1:[],
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            viewVisible:false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            time:{
                page: 1,
                pageSize: 10,
            }
        };
    },
    created() {
        this.getData();
    },
    methods: {
        formatDate(row, column) {
            // 获取单元格数据
            let data = row[column.property]
            if(data == null) {
                return null
            }
            let dt = new Date(data)
                return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds()
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            selectAllComplaints(this.query).then(res => {
                console.log(res.data.list);
                this.tableData = res.data.list;
                this.pageTotal = res.data.total;
            });
        },
        //将时间戳转为yyyy-MM-dd HH:mm:ss
        getRealTime(time){
            return new Date(time + 1000 * 60 * 60 * 8);

        },
        fDate(row, column) {
            // 获取单元格数据
            let data = row[column.property]
            if(data == null) {
                return null
            }
            let dt = new Date(data + 1000 * 60 * 60 *8)
                return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds()
        },
        // 触发搜索按钮
        handleSearch() {
            if(this.value1!==null && this.value1!==''){
                this.time.start = this.value1[0];
                this.time.end = this.value1[1];
            } else {
                this.time.start = null;
                this.time.end = null
            }
            if(this.time.start == null && this.time.end == null){
                selectAllComplaints(this.query).then(res => {
                    console.log(res);
                    this.tableData = res.data.list;
                    this.pageTotal = res.data.total;
                 });
            }else{
                selectAllByTime(this.time).then(res => {
                console.log(res);
                this.tableData = res.data.list;
                this.pageTotal = res.data.total;
            });
            console.log(this.value1);
            this.$set(this.query, 'page', 1);
            }
            
            // this.getData();
        },
          // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = JSON.parse(JSON.stringify(row));
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            modifyAdvice(this.form).then(res => {
                console.log(res);
                this.tableData = res.t.list;
                this.pageTotal = res.t.total || 10;
            if(res.code == 200){
                this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                this.getData();
            }
            }); 
        },
         // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                     delComplaints(row).then(res => {
                            console.log(res);
                        if(res.code === 200){
                            this.$message.success('删除成功');
                            this.getData();
                        }
            });
                })
                .catch(() => {});
        },
          // 查看操作
        handleShow(index, row) {
            this.idx = index;
            this.form = row;
            this.viewVisible = true;
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
